package cn.com.xinjinfu.pdoulcProduct.widget;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

import cn.com.xinjinfu.pdoulcProduct.MainActivity;
import cn.com.xinjinfu.pdoulcProduct.R;
import cn.com.xinjinfu.pdoulcProduct.activity.login.CheckMobileActivity;
import cn.com.xinjinfu.pdoulcProduct.manager.AccountManager;
import cn.com.xinjinfu.pdoulcProduct.manager.ActivityManager;
import cn.com.xinjinfu.pdoulcProduct.manager.ToastManager;
import cn.com.xinjinfu.pdoulcProduct.manager.UIManager;
import cn.com.xinjinfu.pdoulcProduct.utils.LoginRequestUtils;
import cn.com.xinjinfu.pdoulcProduct.utils.SharedPreferencesUtils;

/**
 * 手势绘制/校验界面
 */
public class GestureVerifyActivity extends Activity implements android.view.View.OnClickListener {
    /**
     * 手机号码
     */
    public static final String PARAM_PHONE_NUMBER = "PARAM_PHONE_NUMBER";
    /**
     * 意图
     */
    public static final String PARAM_INTENT_CODE = "PARAM_INTENT_CODE";
    private ImageView mImgUserLogo;
    private TextView mTextPhoneNumber;
    private TextView text_forget_gesture,text_other_account;
    private TextView mTextTip;
    private FrameLayout mGestureContainer;
    private GestureContentView mGestureContentView;
    private TextView mTextForget;
    private TextView mTextOther;
    private String mParamPhoneNumber;
    private long mExitTime = 0;
    private int mParamIntentCode;
    private LockIndicator mLockIndicator;
    private static int PWDNUM = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gesture_verify);
        ObtainExtraData();
        setUpViews();
        setUpListeners();
    }

    private void ObtainExtraData() {
        mParamPhoneNumber = getIntent().getStringExtra(PARAM_PHONE_NUMBER);
        mParamIntentCode = getIntent().getIntExtra(PARAM_INTENT_CODE, 0);
    }

    private void setUpViews() {
//        mImgUserLogo = (ImageView) findViewById(R.id.user_logo);
//        mTextPhoneNumber = (TextView) findViewById(R.id.text_phone_number);
        text_forget_gesture = (TextView) findViewById(R.id.text_forget_gesture);
        text_other_account = (TextView) findViewById(R.id.text_other_account);
        text_forget_gesture.setOnClickListener(this);
        text_other_account.setOnClickListener(this);

        mLockIndicator = (LockIndicator) findViewById(R.id.lock_indicator);
        mTextTip = (TextView) findViewById(R.id.text_tip);
        mGestureContainer = (FrameLayout) findViewById(R.id.gesture_container);
        mGestureContainer.setForegroundGravity(Gravity.CENTER);
        mTextForget = (TextView) findViewById(R.id.text_forget_gesture);
        mTextOther = (TextView) findViewById(R.id.text_other_account);

        String mShoushiPassword = SharedPreferencesUtils.getUserInfo(this, "mShoushiPassword");
        // 初始化一个显示各个点的viewGroup
        if (mShoushiPassword != "") {
            mGestureContentView = new GestureContentView(this, true, mShoushiPassword,
                    new GestureDrawline.GestureCallBack() {

                        @Override
                        public void onGestureCodeInput(String inputCode) {

                        }

                        @Override
                        public void checkedSuccess() {
                            mGestureContentView.clearDrawlineState(0L);
                            mTextTip.setText(Html.fromHtml("<font color='#F77F96'>密码正确</font>"));
                            startActivity(new Intent(GestureVerifyActivity.this, MainActivity.class));
                            GestureVerifyActivity.this.finish();
                        }

                        @Override
                        public void checkedFail() {
                            mGestureContentView.clearDrawlineState(1300L);
                            mTextTip.setVisibility(View.VISIBLE);
                            PWDNUM++;
                            if (PWDNUM == 5){
                                mTextTip.setText(Html.fromHtml("<font color='#c70c1e'>密码错误超过5次，请重新登陆!</font>"));
                            }
                            if (PWDNUM < 5){
                                mTextTip.setText(Html.fromHtml("<font color='#c70c1e'>密码错误，您还可以输入"+(5-PWDNUM)+"次!</font>"));
                            }else {
                                SharedPreferencesUtils.deleteUserInfo(GestureVerifyActivity.this);
                                SharedPreferencesUtils.deletePwdBoolean(GestureVerifyActivity.this);
                                UIManager.showSuccessDilog(GestureVerifyActivity.this, R.drawable.img_doucry, 1, "密码错误超过5次，您将退出登录，请\n重新登录设置新的手势密码。", "确定", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Intent intent = new Intent(GestureVerifyActivity.this, CheckMobileActivity.class);
                                        intent.putExtra("sspwd", "SSPWD");
                                        startActivity(intent);
                                        GestureVerifyActivity.this.finish();
                                    }
                                });
                            }
                            // 左右移动动画
                            Animation shakeAnimation = AnimationUtils.loadAnimation(GestureVerifyActivity.this, R.anim.shake);
                            mTextTip.startAnimation(shakeAnimation);
                        }
                    });
            // 设置手势解锁显示到哪个布局里面
            mGestureContentView.setParentView(mGestureContainer);
            updateCodeList("");
        }
    }

    private void setUpListeners() {
        mTextForget.setOnClickListener(this);
        mTextOther.setOnClickListener(this);
    }

    private void updateCodeList(String inputCode) {
        // 更新选择的图案
        mLockIndicator.setPath(inputCode);
    }
    private String getProtectedMobile(String phoneNumber) {
        if (TextUtils.isEmpty(phoneNumber) || phoneNumber.length() < 11) {
            return "";
        }
        StringBuilder builder = new StringBuilder();
        builder.append(phoneNumber.subSequence(0, 3));
        builder.append("****");
        builder.append(phoneNumber.subSequence(7, 11));
        return builder.toString();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.text_forget_gesture:
                escLogin();
                this.finish();
                break;
            case R.id.text_other_account:
                escLogin();
                this.finish();
                break;
            default:
                break;
        }
    }
    /**退出登录并清空数据*/
    public void escLogin(){
        SharedPreferencesUtils.deleteUserInfo(this);
        SharedPreferencesUtils.deletePwdBoolean(this);
        Intent intent = new Intent(GestureVerifyActivity.this, CheckMobileActivity.class);
        intent.putExtra("sspwd", "SSPWD");
        startActivity(intent);
    }
}
